package com.vitabuybuy.tv.ui.mine.order.adapter

import androidx.core.widget.addTextChangedListener
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ItemOrderCommentBinding
import com.vitabuybuy.tv.dto.OrderDetailDto
import com.library.common.base.BindRecycAdapter

class OrderCommentListAdapter(val stroeName: String?) :
    BindRecycAdapter<ItemOrderCommentBinding, OrderDetailDto.ItemListBean>() {
    private val picAdapter by lazy { CommentPictureAdapter() }
    override fun onBindViewHolder(
        mBinding: ItemOrderCommentBinding,
        itemData: OrderDetailDto.ItemListBean,
        holder: RecyclerView.ViewHolder,
        position: Int
    ) {
        mBinding.itemData = itemData
        mBinding.tvShopName.text = stroeName
        mBinding.ivScore.rating = itemData.scores.toFloat()
        mBinding.etContent.setText(itemData.content)
        if(mBinding.etContent.text.isNotEmpty()){
            mBinding.etContent.setSelection(itemData.content.length)
        }
        mBinding.lvPic.apply {
            adapter = picAdapter
            layoutManager = GridLayoutManager(mContext, 4)
            isNestedScrollingEnabled = false
        }
        picAdapter.setNewData(itemData.fileBean)
        picAdapter.setOnClickDeleteListener {
            mData[position].fileBean.removeAt(it)
            picAdapter.removeAt(it)
        }
        mBinding.btnImage.setOnClickListener {
            onItemPicClick.invoke(position)
        }
        mBinding.btnVideo.setOnClickListener {
            onItemVideoClick.invoke(position)
        }
        mBinding.ivScore.setOnRatingChangeListener { _, rating, _ ->
            mData[position].scores = rating.toInt()
        }
        mBinding.etContent.addTextChangedListener {
            mData[position].content = it.toString()
        }
        picAdapter.setOnItemClickListener { binding, item, childPos ->
            onItemClick.invoke(position,childPos)
        }
    }

    lateinit var onItemPicClick: (position: Int) -> Unit
    lateinit var onItemVideoClick: (position: Int) -> Unit
    lateinit var onItemClick: (parentPos: Int,childPos: Int) -> Unit

    override fun normalViewId(viewType: Int): Int {
        return R.layout.item_order_comment
    }
}
